<?php

class Profile_Model_Data
{
	protected $_ownerClassIdName = NULL;
	protected $_ownerId = NULL;
	protected $_dataArray = array();


	public function __construct($ownerClassIdName = NULL, $ownerId = NULL)
	{
		$this->_ownerClassIdName = $ownerClassIdName;
		$this->_ownerId = $ownerId;
	}


	public function setOwnerClassIdName($objClassIdName = NULL)
	{
		//CHECK: clear the content?
		$this->_ownerClassIdName = $objClassIdName;
		return $this;
	}
	public function setOwnerId($objId =- NULL)
	{
		$this->_ownerId = $objId;
		return $this;
	}
	/**
	 * This method will replace all the data. Use updateDataArray for selective assignment.
	 */
	public function setDataArray(array $dataArray = NULL, $save = TRUE)
	{
		if (is_array($dataArray)) {
			$this->_dataArray = $dataArray;
		} else {
			$this->_dataArray = array();
		}

		//TODO: if the array is empty, clear all values on the DB
		if ($save === TRUE && empty($this->_ownerClassIdName) === FALSE && intval($this->_ownerId) > 0
			&& is_array($dataArray) && count($dataArray) > 0) {

			$table = Profile_Model_DbTable_Data::getInstance();

			//TODO: use atomic query
			foreach ($this->_dataArray as $key => $value) {

				if (empty($key) === TRUE) {
					continue;
				}

				$dbData = array(
					'data_value' => $value,
				);

				$updRows = $table->update($dbData, array(
					'owner_class= ?' => $this->getOwnerClassIdName(),
					'owner_id= ?' => $this->getOwnerId(),
					'data_key= ?' => $key,
				));

				if ($updRows != 1) {
					$dbData['owner_class'] = $this->getOwnerClassIdName();
					$dbData['owner_id'] = $this->getOwnerId();
					$dbData['data_key'] = $key;
					$updRows = $table->insert($dbData);
				}
			}
		}

		return $this;
	}

	public function setData($key = NULL, $value = NULL, $save = TRUE)
	{
		if (empty($key) === FALSE) {
			$this->_dataArray[$key] = $value;

			if ($save === TRUE && empty($this->_ownerClassIdName) === FALSE && intval($this->_ownerId) > 0) {

				$table = Profile_Model_DbTable_Data::getInstance();

				$dbData = array(
					'data_value' => $value,
				);

				$updRows = $table->update($dbData, array(
					'owner_class= ?' => $this->getOwnerClassIdName(),
					'owner_id= ?' => $this->getOwnerId(),
					'data_key= ?' => $key,
				));

				if ($updRows != 1) {
					$dbData['owner_class'] = $this->getOwnerClassIdName();
					$dbData['owner_id'] = $this->getOwnerId();
					$dbData['data_key'] = $key;
					$updRows = $table->insert($dbData);
				}
			}
		}
	}
	public function updateDataArray(array $dataArray = NULL, $save = TRUE)
	{
		if (is_array($dataArray) === FALSE || count($dataArray) === 0) {
			return $this;
		}

		if (empty($this->_ownerClassIdName) === FALSE && intval($this->_ownerId) > 0) {

			if ($save === TRUE) {
				$table = Profile_Model_DbTable_Data::getInstance();
			}

			//TODO: use atomic query
			foreach ($this->_dataArray as $key => $value) {

				if (empty($key) === TRUE) {
					continue;
				}

				$this->_dataArray[$key] = $value;

				if ($save === TRUE) {
					$dbData = array(
						'data_value' => $value,
					);

					$updRows = $table->update($dbData, array(
						'owner_class= ?' => $this->getOwnerClassIdName(),
						'owner_id= ?' => $this->getOwnerId(),
						'data_key= ?' => $key,
					));

					if ($updRows != 1) {
						$dbData['owner_class'] = $this->getOwnerClassIdName();
						$dbData['owner_id'] = $this->getOwnerId();
						$dbData['data_key'] = $key;
						$updRows = $table->insert($dbData);
					}
				}
			}
		}

		return $this;
	}


	public function getOwnerClassIdName()
	{
		return $this->_ownerClassIdName;
	}
	public function getOwnerId()
	{
		return $this->_ownerId;
	}
	public function getDataArray()
	{
		return $this->_dataArray;
	}

	public function getData($key = NULL)
	{
		if (empty($key) === FALSE && isset($this->_dataArray[$key])) {
			return $this->_dataArray[$key];
		}
		return NULL;
	}
	public function get($key = NULL)
	{
		if (empty($key) === FALSE && isset($this->_dataArray[$key])) {
			return $this->_dataArray[$key];
		}
		return NULL;
	}



	protected static $_instanceList = array();


	/**
	 * Queries for a loaded instance.
	 */
	public static function findInstance($objClassIdName, $objId)
	{
		if (empty($objClassIdName) === FALSE && intval($objId) > 0 &&
			isset(self::$_instanceList[$objClassIdName]) && isset(self::$_instanceList[$objClassIdName][$objId])) {
			return self::$_instanceList[$objClassIdName][$objId];
		}
		return NULL;
	}

	/**
	 * Queries for an instance. If the instance is not found, it will be loaded from the database.
	 */
	public static function getInstance($objClassIdName, $objId)
	{
		if (empty($objClassIdName) || intval($objId) < 1) {
			return NULL;
		}


		$instance = self::findInstance($objClassIdName, $objId);

		if ($instance instanceof Profile_Model_Data) {
			return $instance;
		}


		$instance = new Profile_Model_Data();


		$table = Profile_Model_DbTable_Data::getInstance();

		$select = $table->select();
		$select
			->where('owner_class= ?', $objClassIdName)
			->where('owner_id= ?', $objId)
		;

		$dbResult = $table->fetchAll($select);

		$pData = array();
		foreach ($dbResult as $dbRow) {
			$pData[$dbRow->data_key] = $dbRow->data_value;
		}


		$instance
			->setOwnerClassIdName($objClassIdName)
			->setOwnerId($objId)
			->setDataArray($pData, FALSE)
		;

		self::$_instanceList[$objClassIdName][$objId] = $instance;


		return $instance;
	}
}
